package frontend.chart;
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

public class MemoryUsageDemo extends JPanel {

	private XYPlot xyplot;

	class DataGenerator extends Timer implements ActionListener {

		private boolean startFlg = true;

		public void actionPerformed(ActionEvent actionevent) {
			long l = Runtime.getRuntime().freeMemory();
			long l1 = Runtime.getRuntime().totalMemory();
			Calendar cal = Calendar.getInstance();
			// if(startFlg) {
			// for (int i=0;i<10;i++) {
			// cal.add(Calendar.SECOND,i - 10);
			// Millisecond millisecond = new Millisecond(cal.getTime());
			// addTotalObservation(millisecond, l1);
			// addFreeObservation(millisecond, l);
			// }
			// } else {
			cal.set(Calendar.MILLISECOND, 0);
//			Date date = 
			Millisecond millisecond = new Millisecond(cal.getTime());
			addTotalObservation(millisecond, l1);
			addFreeObservation(millisecond, l);
			// }
			// startFlg = false;
		}

		DataGenerator(int i) {
			super(i, null);
			addActionListener(this);
		}
	}

	private TimeSeries total;
	private TimeSeries free;

	public MemoryUsageDemo() {
		super(new BorderLayout());
		total = new TimeSeries("Total Memory");
		total.setMaximumItemCount(10);
		// total.setMaximumItemAge(i);
		free = new TimeSeries("Free Memory");
		// free.setMaximumItemAge(i);
		free.setMaximumItemCount(10);
		TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
		timeseriescollection.addSeries(total);
		timeseriescollection.addSeries(free);
		DateAxis dateaxis = new DateAxis("Time");
		NumberAxis numberaxis = new NumberAxis("Memory");
		numberaxis.setUpperBound(7000000);
		dateaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
		numberaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
		dateaxis.setLabelFont(new Font("SansSerif", 0, 14));
		numberaxis.setLabelFont(new Font("SansSerif", 0, 14));
		XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer(
				true, false);
		xylineandshaperenderer.setSeriesPaint(0, Color.red);
		xylineandshaperenderer.setSeriesPaint(1, Color.green);
		xylineandshaperenderer.setSeriesStroke(0, new BasicStroke(3F, 0, 2));
		xylineandshaperenderer.setSeriesStroke(1, new BasicStroke(3F, 0, 2));
		//设置数据点可见
		xylineandshaperenderer.setShapesVisible(true);
		xylineandshaperenderer.setShapesFilled(true); 
		xylineandshaperenderer.setShape(new java.awt.geom.Ellipse2D.Double(-2, -2, 4, 4)); 
		xyplot = new XYPlot(timeseriescollection, dateaxis, numberaxis,
				xylineandshaperenderer);
		dateaxis.setAutoRange(true);
		// dateaxis.setDateFormatOverride(new SimpleDateFormat("hh:mm:ss"));
		dateaxis.setVerticalTickLabels(true);
		dateaxis.setLowerMargin(0.0D);
		dateaxis.setUpperMargin(0.0D);
		dateaxis.setTickLabelsVisible(true);
		// numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		numberaxis.setAutoTickUnitSelection(false);
		// 设置纵坐标值的间距为10
		numberaxis.setTickUnit(new NumberTickUnit(5000000));
		// 纵坐标值只能是0-100之间的值
		numberaxis.setRangeWithMargins(0, 80000000);
		
		
//		numberaxis.setAutoTickUnitSelection(false);
//
//        numberaxis.setTickUnit(new NumberTickUnit(数值));
		JFreeChart jfreechart = new JFreeChart("JVM Memory Usage", new Font(
				"SansSerif", 1, 24), xyplot, true);
		ChartUtilities.applyCurrentTheme(jfreechart);
		ChartPanel chartpanel = new ChartPanel(jfreechart, true);
		chartpanel.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createEmptyBorder(4, 4, 4, 4),
				BorderFactory.createLineBorder(Color.black)));
		add(chartpanel);
	}

	private void addTotalObservation(Millisecond millisecond, double d) {
		if (total.getItemCount() > 1) {
			SimpleDateFormat frm = new SimpleDateFormat("hh:mm:ss");
			DateAxis dateaxiss = (DateAxis) xyplot.getDomainAxis();
			int count = 1;
			// 时间轴间距是5分钟,格式为小时:分钟
			dateaxiss.setTickUnit(new DateTickUnit(DateTickUnit.SECOND, count,
					frm));

		}
		total.add(millisecond, d);
	}

	private void addFreeObservation(Millisecond millisecond, double d) {
		free.add(millisecond, d);
	}

	public static void main(String args[]) {
		JFrame jframe = new JFrame("Memory Usage Demo");
		MemoryUsageDemo memoryusagedemo = new MemoryUsageDemo();
		jframe.getContentPane().add(memoryusagedemo, "Center");
		jframe.setBounds(200, 120, 1000, 280);
		jframe.setVisible(true);
		(memoryusagedemo.new DataGenerator(1000)).start();
		jframe.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent windowevent) {
				System.exit(0);
			}

		});
	}

}
